//
//  AddSubViewController.m
//  CarTown
//
//  Created by 熊浩 on 15/9/3.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "AddSubViewController.h"
#import "PhoneNumberViewController.h"
#import "Puser.h"
#import "UserInfoUpdateResde.h"
@interface AddSubViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *name;
@property (weak, nonatomic) IBOutlet UITextField *phone;
@property (weak, nonatomic) IBOutlet UITextField *subusername;
@property (weak, nonatomic) IBOutlet UILabel *bindphone;
@property (weak, nonatomic) IBOutlet UITextField *companyjob;
@property (weak, nonatomic) IBOutlet UITextField *password;
@property (nonatomic,strong) NSArray *textarray;
@end

@implementation AddSubViewController
- (IBAction)showpass:(id)sender {
    self.password.secureTextEntry=!self.password.secureTextEntry;
}
+(instancetype)viewcontroller{
    AddSubViewController *vc=[[AddSubViewController alloc]initWithNibName:NSStringFromClass([self class]) bundle:nil];
    return vc;
}
- (IBAction)bindphone:(id)sender {
    PhoneNumberViewController *pvc=[PhoneNumberViewController Viewcontroller];
    pvc.phonetext=self.bindphone.text;
    pvc.block=^(NSString *phone){
        self.bindphone.text=phone;
        self.bindphone.hidden=NO;
    };
    [self.navigationController pushViewController:pvc animated:YES];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title=@"添加业务员";
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStyleDone target:self action:@selector(rightclick)];
    self.textarray=@[self.name,self.companyjob,self.subusername,self.phone,self.password];
}
-(void)rightclick{
    if ([self checkFrom]) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        
        hud.labelText=@"正在添加";
        
        hud.margin = 10.f;
        
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];

        NSMutableDictionary *params=[NSMutableDictionary dictionary];
        params[@"name"]=self.name.text;
        params[@"job"]=self.companyjob.text;
        params[@"username"]=self.subusername.text;
        params[@"passwd"]=self.password.text;
        params[@"phone"]=self.phone.text;
        if (!self.bindphone.hidden) {
            params[@"bphone"]=self.bindphone.text;
        }
        Puser *user=[Puser getUser];
        params[@"action"]=@"c_child_add";
        params[@"user_type"]=@"company";
        params[@"com_id"]=user.com_id;
       [PostUitls Post:userinfourl parameters:params success:^(id response) {
           hud.mode = MBProgressHUDModeText;

           UserInfoUpdateResde *res=[UserInfoUpdateResde resdeWithDict:response];
           if ([res.state intValue]==400) {
               hud.labelText=@"添加成功";
               [hud hide:YES afterDelay:1];
               [self.navigationController popViewControllerAnimated:YES];
           }else{
               hud.labelText=@"添加失败";
               [hud hide:YES afterDelay:1];
           }
       } failure:^(NSError *error) {
           hud.mode = MBProgressHUDModeText;
           hud.labelText=NetWorkError;
           [hud hide:YES afterDelay:1];
       }];
    }
}
-(BOOL)checkFrom{
    [self.view becomeFirstResponder];
    if ([self.name.text isEqualToString:@""]) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        
        hud.labelText=@"业务员姓名不能为空";
        hud.mode = MBProgressHUDModeText;
        
        hud.margin = 10.f;
        
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return NO;

    }
    if ([self.companyjob.text isEqualToString:@""]) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        
        hud.labelText=@"职位不能为空";
        hud.mode = MBProgressHUDModeText;
        
        hud.margin = 10.f;
        
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return NO;
    }
    if ([self.subusername.text isEqualToString:@""]) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        
        hud.labelText=@"用户名不能为空";
        hud.mode = MBProgressHUDModeText;
        
        hud.margin = 10.f;
        
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return NO;
    }
    if ([self.phone.text isEqualToString:@""]) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        
        hud.labelText=@"联系电话不能为空";
        hud.mode = MBProgressHUDModeText;
        
        hud.margin = 10.f;
        
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return NO;
    }
    if ([self.companyjob.text isEqualToString:@""]) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        
        hud.labelText=@"登录密码不能为空";
        hud.mode = MBProgressHUDModeText;
        
        hud.margin = 10.f;
        
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return NO;
    }
    return YES;
    
    
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    NSLog(@"%ld",textField.tag);
    if (textField.tag==4) {
        [textField endEditing:YES];
        return YES;
    }
    UITextField *textf=self.textarray[textField.tag+1];
    [textf becomeFirstResponder];
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
